{# itop-portal-base/portal/src/views/bricks/create/layout.html.twig #}
{# Create brick base layout #}
{% extends 'itop-portal-base/portal/src/views/modal/layout.html.twig' %}

{% block pModalTitle %}
    {{ oBrick.GetTitle()|dict_s }}
{% endblock %}

{% block pModalBody %}
	<p>{{ 'Brick:Portal:Create:ChooseType'|dict_s }}</p>
	<ul id="{{ sBrickId }}_leaf_classes">
        {% for aLeafClass in aLeafClasses %}
			<li><a href="#" data-target-class="{{ aLeafClass.id }}">{{ aLeafClass.name }}</a></li>
        {% endfor %}
	</ul>

	<script type="text/javascript">
        $(document).ready(function(){
            $('#{{ sBrickId }}_leaf_classes a').off('click').on('click', function(oEvent){
                oEvent.preventDefault();

                var sUrl = '{{ app['url_generator'].generate('p_object_create', {sObjectClass : '-sObjectClass-'})|raw }}';
                var oModalElem = $(this).closest('.modal');
                // Showing loader
                oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
                // Preparing target class url
                sUrl = sUrl.replace(/-sObjectClass-/, $(this).attr('data-target-class') );
                sUrl = AddParameterToUrl(sUrl, 'ar_token', '{{ ar_token }}');
                // Loading form
                oModalElem.find('.modal-content').load(sUrl, function(oData, sStatus, oXHR){
                    var oResponse = (oXHR.responseJSON !== undefined) ? oXHR.responseJSON : JSON.parse(oXHR.responseText);

                    // Note : This could be refactored for a global use
                    oModalElem.html( $('#modal-for-alert').html() );
                    oModalElem.find('.modal-title').html(oResponse.error_title);
                    oModalElem.find('.modal-body .alert').html(oResponse.error_message)
                        .removeClass('alert-success alert-info alert-warning alert-danger')
                        .addClass('alert-danger');
                });
            });
        });
	</script>
{% endblock %}

{% block pModalFooter %}
	<button type="button" class="btn btn-default" data-dismiss="modal">{{ 'Portal:Button:Cancel'|dict_s }}</button>
{% endblock %}